package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.Forum;
import com.danco.training.zaneuskaya.domain.Note;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.service.ForumService;
import com.danco.training.zaneuskaya.service.NoteService;
import com.danco.training.zaneuskaya.service.UserService;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("session")
public class ForumBean implements Serializable {

	private static final String NO_CURRENT_FORUM_OR_USER_DOESN_T_LOGGED_IN = "No current forum or user doesn't logged in";

	private static final String FORUM_INFO_FACES_REDIRECT_TRUE_PAGE = "/forum/info?faces-redirect=true";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(ForumBean.class);

	private List<User> forumParticipants;

	private List<Forum> forumList;

	private List<Note> forumNotes;

	private Forum currentForum;

	private int forumId;

	@Autowired
	private NoteService noteService;

	@Autowired
	private UserService userService;

	@Autowired
	private ForumService forumService;

	@Autowired
	private UserBean userBean;

	private boolean participant;

	public boolean isParticipant() {
		if (currentForum == null || userBean.getSelf() == null) {
			LOGGER.info(NO_CURRENT_FORUM_OR_USER_DOESN_T_LOGGED_IN);
			return false;
		} else {
			return forumService.isMember(currentForum, userBean.getSelf());
		}

	}

	public void setParticipant(boolean participant) {
		this.participant = participant;
	}

	public NoteService getNoteService() {

		return noteService;
	}

	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}

	public List<Note> getForumNotes() {
		forumNotes = noteService.findForumNoteList(currentForum);
		return forumNotes;
	}

	public void setForumNotes(List<Note> forumNotes) {
		this.forumNotes = forumNotes;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public List<User> getForumParticipants() {
		forumParticipants = userService.getForumParticipants(currentForum);
		return forumParticipants;
	}

	public void setForumParticipants(List<User> forumParticipants) {
		this.forumParticipants = forumParticipants;
	}

	public Forum getCurrentForum() {
		currentForum = forumService.getEntityById(currentForum.getForumId());
		return currentForum;
	}

	public void setCurrentForum(Forum currentForum) {

		this.currentForum = currentForum;
	}

	public int getForumId() {
		return forumId;
	}

	public void setForumId(int forumId) {
		currentForum = forumService.getEntityById(forumId);
		this.forumId = forumId;
	}

	public List<Forum> getForumList() {
		forumList = forumService.getEntityList();
		return forumList;
	}

	public void setForumList(List<Forum> forumList) {
		this.forumList = forumList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ForumService getForumService() {
		return forumService;
	}

	public void setForumService(ForumService forumService) {
		this.forumService = forumService;
	}

	public String addMember() {
		forumService.addForumMember(currentForum, userBean.getSelf());
		return FORUM_INFO_FACES_REDIRECT_TRUE_PAGE;
	}

}
